//
//  contextualView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/15.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "contextualView.h"
#import "createNewSceneView.h"
#import "sceneContentView.h"

@interface contextualView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    NSDictionary *jsonDic;
    NSString *sceneListPath;
    NSMutableArray *mySceneArray;
    UIView *nowView;
    NSUInteger now_scene_tag;
    NSString *scene_name;
}

@property (nonatomic, strong) UICollectionView *myCollectionView;

@end

@implementation contextualView

- (UICollectionView *)myCollectionView
{
    if(!_myCollectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(90, 90);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumInteritemSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,self.bounds.size.width, self.bounds.size.height)
                                               collectionViewLayout:layout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        
        _myCollectionView.backgroundColor = [UIColor whiteColor];
        [_myCollectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    }
    return _myCollectionView;
}

- (void)analysisJsonDic
{
    mySceneArray = [[NSMutableArray alloc] initWithContentsOfFile:sceneListPath];
    if(!mySceneArray){
        mySceneArray = [[NSMutableArray alloc] init];
    }
}

- (void)sendMessageBySceneID:(NSUInteger)sceneID
{
    [self sendMessage:[NSString stringWithFormat:@"EVENT-RUN-%lu",(unsigned long)sceneID]];
}

- (void)sendMessage:(NSString *)messgae
{
    self.sendMessage(messgae);
}

- (void)hideNowView
{
    [nowView removeFromSuperview];
    nowView = nil;
}

- (void)updateData
{
    createNewSceneView *dataView = (createNewSceneView *)nowView;
    if([dataView.myTextField.text isEqualToString:@""]){
        dataView.myTextField.text = @"场景";
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:dataView.imageName, @"imageName", dataView.myTextField.text, @"name", nil];
    if(dic){
        [mySceneArray addObject:dic];
        BOOL b = [mySceneArray writeToFile:sceneListPath atomically:YES];
        NSLog(@"%@", b? @"yes":@"no");
    }
    
    [self.sceneDelagate showSceneView];
    
    [self analysisJsonDic];
    [self.myCollectionView reloadData];
}

- (void)addFundation
{
    sceneContentView *contentView = (sceneContentView *)nowView;
    [contentView addFundation];
}

- (void)showSceneContentView
{
    [self.sceneDelagate setIsShowContentView:NO];
    [self.sceneDelagate hideSceneLeftView];
    [self.sceneDelagate showSceneLeftView];
    [self.sceneDelagate showSceneTitle:scene_name];
    sceneContentView *s_view = (sceneContentView *)nowView;
    [s_view hideFundationView];
    [self.sceneDelagate showSceneRightButton:1];
}

- (void)changeFunction:(NSArray *)function
{
    [self showSceneContentView];
    NSDictionary *dic = [mySceneArray objectAtIndex:now_scene_tag];
    [mySceneArray removeObject:dic];;
    NSDictionary *dic_new = [NSDictionary dictionaryWithObjectsAndKeys:[dic objectForKey:@"imageName"], @"imageName",
                             [dic objectForKey:@"name"], @"name",
                             function, @"functions", nil];
    [mySceneArray insertObject:dic_new atIndex:now_scene_tag];
    BOOL b = [mySceneArray writeToFile:sceneListPath atomically:YES];
    NSLog(@"%@", b? @"yes":@"no");
}

#pragma mark - UICollectionViewDataSource 和 UICollectionViewDelegate协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [mySceneArray count] + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if(Cell == nil){
        
    }
    
    if(indexPath.row < [mySceneArray count]){
        Cell.label.text = [NSString stringWithString:[[mySceneArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
        NSString *imageName = [[mySceneArray objectAtIndex:indexPath.row] objectForKey:@"imageName"];
        [Cell setImageViewWithImage:GET_IAMGE_WITH_NAME(imageName)];
    }else{
        Cell.label.text = @"添加";
        [Cell setImageViewWithImage:GET_IAMGE_WITH_NAME(@"Image/img_addscene")];
    }
    [Cell addSubview:Cell.imageView];
    [Cell addSubview:Cell.label];
    
    return Cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCollectionViewCell *cell = (myCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
    if(indexPath.row < [mySceneArray count]){
        now_scene_tag = indexPath.row;
        NSArray *array = [[mySceneArray objectAtIndex:now_scene_tag] objectForKey:@"functions"];
        sceneContentView *c = [[sceneContentView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height + 70) andFundationArray:array];
        c.jsonDic = jsonDic;
        __weak contextualView *m = self;
        c.getAllFunction = ^(NSArray *allFuction){
            [m changeFunction:allFuction];
        };
        c.sendFundationMessage = ^(NSString *str){
            m.sendMessage(str);
        };
        [self addSubview:c];
        nowView = c;
        scene_name = cell.label.text;
        [self.sceneDelagate showSceneTitle:scene_name];
        [self.sceneDelagate showSceneRightButton:1];
    }else{
        createNewSceneView *createView = [[createNewSceneView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height + 70)];
        [self addSubview:createView];
        nowView = createView;
        [self.sceneDelagate showSceneTitle:@"添加场景"];
        [self.sceneDelagate showSceneRightButton:0];
    }
    [self.sceneDelagate showSceneLeftView];
    [self.sceneDelagate hideTabView];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //myCollectionViewCell *cell = (myCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
    if(indexPath.row < [mySceneArray count]){
        
    }else{
        
    }
}


- (id) initWithFrame:(CGRect)frame andDic:(NSDictionary *)dic
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = BACKGROUND_COLOR;
        jsonDic = dic;
        sceneListPath = [NSHomeDirectory() stringByAppendingPathComponent:SCENE_LIST_PATH];
        [self analysisJsonDic];
        [self addSubview:self.myCollectionView];
    }
    return self;
}

@end
